package com.bestescortbedsystem.service.impl;

import com.bestescortbedsystem.dao.SesUserDao;
import com.bestescortbedsystem.dao.SesUserSharingMessageDao;
import com.bestescortbedsystem.entity.SesUser;
import com.bestescortbedsystem.entity.SesUserExample;
import com.bestescortbedsystem.entity.SesUserSharingMessage;

import com.bestescortbedsystem.service.MessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    private SesUserDao sesUserDao;
    @Resource
    private SesUserSharingMessageDao sesUserSharingMessageDao;
    @Override
    public Map<String,Object> getAllMessage(String userType,String messagetype,String pageNumber, String pageSize) {
        List<SesUserSharingMessage> list=new ArrayList<SesUserSharingMessage>();
        PageHelper.startPage(Integer.parseInt(pageNumber), Integer.parseInt(pageSize));
        if (userType==null||userType.equals("")){
            list = sesUserSharingMessageDao.selectAll1(messagetype);
        }else {
            list = sesUserSharingMessageDao.selectAll(Integer.parseInt(userType), messagetype);
        }

        PageInfo<SesUserSharingMessage> sesUserSharingMessagePageInfo=new PageInfo<>(list);
        List<SesUserSharingMessage> list1=sesUserSharingMessagePageInfo.getList();
        System.out.println("*****************************");
        for (SesUserSharingMessage sm:list) {
            System.out.println(sm);
        }
        System.out.println("*****************************");
        Map<String,Object> map=new HashMap<>();
        map.put("xiaoxi",list1);
        map.put("page",sesUserSharingMessagePageInfo);
        return map;
    }

    @Override
    public void deleteMessage(String messageid) {
        sesUserSharingMessageDao.deleteByPrimaryKey(Long.parseLong(messageid));
    }

    @Override
    public void addMessage(String userType, String messagetype, Integer messagesendtype, String messagetitle, String messagecontent) {
        SesUserExample sesUserExample=new SesUserExample();
        SesUserExample.Criteria criteria=sesUserExample.createCriteria();
        criteria.andUsertypeEqualTo(Integer.parseInt(userType));
        List<SesUser> sesUserList=sesUserDao.selectByExample(sesUserExample);
        for (SesUser su:sesUserList) {
            Long a=su.getUserid();
            System.out.println("2222222222222222222222222222222222222222222222222222222222222222");
            System.out.println(a);
            SesUserSharingMessage sesUserSharingMessage=new SesUserSharingMessage();
            sesUserSharingMessage.setUserreceivedid(a);
            sesUserSharingMessage.setMessagetype(messagetype);
            sesUserSharingMessage.setMessagesendtype(messagesendtype);
            sesUserSharingMessage.setMessagetitle(messagetitle);
            sesUserSharingMessage.setMessagecontent(messagecontent);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            sesUserSharingMessage.setMessagetime(sdf.format(new Date()));
            sesUserSharingMessageDao.insertSelective(sesUserSharingMessage);
        }


    }


}
